package com.ghh.test;

import com.aliyuncs.exceptions.ClientException;
import com.ghh.utils.SMSUtils;
import com.ghh.utils.ValidateCodeUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class demo {

    @Test
   public void test1() throws IOException {
        XSSFWorkbook sheets = new XSSFWorkbook("C:\\Users\\GHH\\Desktop\\demo.xlsx");
        XSSFSheet sheetAt = sheets.getSheetAt(0);
        for (Row cells : sheetAt) {
            for (Cell cell : cells) {
                String stringCellValue = cell.getStringCellValue();
                System.out.println(stringCellValue);
            }
        }
        sheets.close();
    }

    @Test
    public void test2(){
        try {
        String s = String.valueOf(ValidateCodeUtils.generateValidateCode(6));
        System.out.println(s);
        SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE,"13164604969",s);
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test3() throws Exception {
        //1.创建配置类
        Configuration configuration=new Configuration(Configuration.getVersion());
        //2.设置模板所在的目录
        configuration.setDirectoryForTemplateLoading(new File("G:\\ftl"));
        //3.设置字符集
        configuration.setDefaultEncoding("utf-8");
        //4.加载模板
        Template template = configuration.getTemplate("test.ftl");
        //5.创建数据模型
        Map map=new HashMap();
        map.put("name", "张三");
        map.put("message", "欢迎来到传智播客！");
        map.put("success", true);
        List goodsList=new ArrayList();
        Map goods1=new HashMap();
        goods1.put("name", "苹果");
        goods1.put("price", 5.8);
        Map goods2=new HashMap();
        goods2.put("name", "香蕉");
        goods2.put("price", 2.5);
        Map goods3=new HashMap();
        goods3.put("name", "橘子");
        goods3.put("price", 3.2);
        goodsList.add(goods1);
        goodsList.add(goods2);
        goodsList.add(goods3);
        map.put("goodsList", goodsList);
        //6.创建Writer对象
        Writer out =new FileWriter(new File("G:\\test.html"));
        //7.输出
        template.process(map, out);
        //8.关闭Writer对象
        out.close();
    }
}

